/*
 ============================================================================
 Name		: FEAppContainer.cpp
 Author	  : jk
 Copyright   : Your copyright notice
 Description : Application Container implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include "FEAppDriverContainer.h" 
#include <BARSREAD.H>
#include <FE_0xE986B88E.rsg>
#include <akniconarray.h>
#include <Icon.mbg>
#include <eikclbd.h>
#include <aknnotewrappers.h> 
#include <EIKLBO.H>
#include "FE.hrh"

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CFEAppDriverContainer::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CFEAppDriverContainer* CFEAppDriverContainer::NewL(const TRect& aRect)
{
	CFEAppDriverContainer* self = CFEAppDriverContainer::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
}

// -----------------------------------------------------------------------------
// CFEAppDriverContainer::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CFEAppDriverContainer* CFEAppDriverContainer::NewLC(const TRect& aRect)
{
	CFEAppDriverContainer* self = new (ELeave) CFEAppDriverContainer;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
}

// -----------------------------------------------------------------------------
// CFEAppDriverContainer::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CFEAppDriverContainer::ConstructL(const TRect& aRect)
{
	// Create a window for this application Container
	CreateWindowL();

	iListbox = new (ELeave) CAknDoubleLargeStyleListBox;
	CleanupStack::PushL(iListbox);
	iListbox->SetContainerWindowL(*this);
	iListbox->SetListBoxObserver(this);

	TResourceReader re;
	iEikonEnv->CreateResourceReaderLC(re, R_DRIVER_LISTBOX);
	iListbox->ConstructFromResourceL(re);
	CleanupStack::PopAndDestroy();
	CleanupStack::Pop();

	iListbox->CreateScrollBarFrameL();
	iListbox->ScrollBarFrame()->SetScrollBarVisibilityL(
			CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);

	TFileName IconFileName;
	IconFileName.Append(_L("\\resource\\apps\\Icon.mbm"));

	// Create an array of icons, reading them from the file
	CArrayPtr<CGulIcon>* icons = new (ELeave) CAknIconArray(4);
	CleanupStack::PushL(icons);

	//show disk tag
	icons->AppendL(iEikonEnv->CreateIconL(IconFileName, EMbmIcon1,
			EMbmIcon1_mask));

	CleanupStack::Pop(icons);
	iListbox->ItemDrawer()->ColumnData()->SetIconArray(icons);

	CTextListBoxModel* model = iListbox->Model();
	model->SetOwnershipType(ELbmOwnsItemArray);
	CDesCArray* mainItemsArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());

	//show data 
	TBuf<50> bufItem;
	_LIT(Form_disk,"0\t%c:\t");

	//scan the available disc
	TDriveList aList;
	RFs rfs;
	rfs.Connect(KFileServerDefaultMessageSlots);
	rfs.DriveList(aList);

	TChar driveLetter;
	TInt64 free_c;
	TBuf<10> sizeWU;

	for (TInt i = EDriveA; i <= EDriveZ; i++)
	{
		if (aList[i])
		{
			free_c = GetDriveInfo(i, rfs);
			RFs::DriveToChar(i, driveLetter);
			TransSize(free_c, sizeWU);
			bufItem.Format(Form_disk, driveLetter);
			bufItem.Append(_L("Free: "));
			bufItem.Append(sizeWU);
			mainItemsArray->AppendL(bufItem);
			sizeWU.Zero();
			bufItem.Zero();
		}
	}
	rfs.Close();
	SetRect(aRect);
	ActivateL();
}

// -----------------------------------------------------------------------------
// CFEAppDriverContainer::CFEAppDriverContainer()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CFEAppDriverContainer::CFEAppDriverContainer()
{
	// No implementation required
}

// -----------------------------------------------------------------------------
// CFEAppDriverContainer::~CFEAppDriverContainer()
// Destructor.
// -----------------------------------------------------------------------------
//
CFEAppDriverContainer::~CFEAppDriverContainer()
{
	delete iListbox;
}

// -----------------------------------------------------------------------------
// CFEAppDriverContainer::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CFEAppDriverContainer::Draw(const TRect& /*aRect*/) const
{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();

	// Gets the control's extent
	TRect drawRect(Rect());

	// Clears the screen
	gc.Clear(drawRect);

}

// -----------------------------------------------------------------------------
// CFEAppDriverContainer::SizeChanged()
// Called by framework when the Container size is changed.
// -----------------------------------------------------------------------------
//
void CFEAppDriverContainer::SizeChanged()
{
	TRect rect = Rect();
	iListbox->SetExtent(rect.iTl, rect.Size());
}

// -----------------------------------------------------------------------------
// CFEAppDriverContainer::HandlePointerEventL()
// Called by framework to handle pointer touch events.
// Note: although this method is compatible with earlier SDKs, 
// it will not be called in SDKs without Touch support.
// -----------------------------------------------------------------------------
//
void CFEAppDriverContainer::HandlePointerEventL(
		const TPointerEvent& aPointerEvent)
{

	// Call base class HandlePointerEventL()
	CCoeControl::HandlePointerEventL(aPointerEvent);
}

TInt CFEAppDriverContainer::CountComponentControls() const
{
	return 1; // return nbr of controls inside this container
}

CCoeControl* CFEAppDriverContainer::ComponentControl(TInt aIndex) const
{
	switch (aIndex)
	{
		case 0:
			return iListbox;
		default:
			return NULL;
	}
}

TKeyResponse CFEAppDriverContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
		TEventCode aType)
{
	if (iListbox != NULL)
	{
		return iListbox->OfferKeyEventL(aKeyEvent, aType);
	}
	else
	{
		return EKeyWasNotConsumed;
	}
}

void CFEAppDriverContainer::HandleListBoxEventL(CEikListBox* aListBox,
		TListBoxEvent aEventType)
{
	if ((aEventType == MEikListBoxObserver::EEventEnterKeyPressed)
			|| (aEventType == MEikListBoxObserver::EEventItemClicked))
	{
		TUid diskId;
		TBuf8<30> path;
		TInt index = aListBox->CurrentItemIndex();
		GetSelectedDriveString(index, path);
		iView->GetAppUi()->ActivateLocalViewL(TUid::Uid(EDirView), diskId, path);
	}
}

/*****************************************************************************
 * @Purpose: Get free volume of the appointed disc 
 * if the disk is aviable ,return the volume of the disk with unit 'MB', else return '-1'
 * @Parameters: aDriveNum -- the enum variable of the drive
 * 			 	&rfs -- a RFs object
 * @Returns TInt
 *****************************************************************************/
TInt64 CFEAppDriverContainer::GetDriveInfo(TInt aDriveNum, RFs &aRfs)
{
	TVolumeInfo volumeInfo;
	TInt err = aRfs.Volume(volumeInfo, aDriveNum);
	if (err != KErrNotReady)
	{
		return volumeInfo.iFree;
	}
	else
	{
		return -1;
	}
}

/*****************************************************************************
 * @Purpose: Get the text of the item which is seleted currently in the iListbox
 * 		return descriptor which points to the text of the selected item
 * @Parameters: index--the index which is selected
 * @Returns: TPtrC
 *****************************************************************************/
TPtrC CFEAppDriverContainer::GetSelectedItems(TInt aIndex)
{
	return iListbox->Model()->ItemText(aIndex);
}

/*****************************************************************************
 * @Purpose: Resolve for the text of the selected item to get the disc tag
 * @Parameters: aIndex--the index which is selected
 * 				 aPath-- descriptor which is used to store the disc tag
 * @Returns: void
 *****************************************************************************/
void CFEAppDriverContainer::GetSelectedDriveString(TInt aIndex, TDes8& aPath)
{
	TPtrC aResult = GetSelectedItems(aIndex);
	TInt indexOfDivice = aResult.Locate(':');
	aPath.Copy(aResult);
	aPath.Replace(0, indexOfDivice - 1, _L8(""));
	aPath.Replace(1, aPath.Length() - 1, _L8(""));
	aPath.Trim();
	aPath.Append(_L(":\\"));
}
/*****************************************************************************
 * @Purpose: get the value of the member variable 'iListbox'
 * @Returns: CAknDoubleLargeStyleListBox*
 *****************************************************************************/
CAknDoubleLargeStyleListBox *CFEAppDriverContainer::GetListBoxCtrl()
{
	return iListbox;
}
/*****************************************************************************
 * @Purpose: set the value of view
 * @Parameters:aView--a pointer of a CFEAppDriverView
 * @Purpose: void
 *****************************************************************************/
void CFEAppDriverContainer::SetView(CFEAppDriverView *aView)
{
	iView = aView;
}
/*****************************************************************************
 * @Purpose: set the value of view
 * @Parameters:aSize--size of the disc
 * 			   aBuf--a descriptor which is use to store the disc tag and the size number 
 * @Purpose: void
 *****************************************************************************/
void CFEAppDriverContainer::TransSize(TInt64 aSize, TDes &aBuf)
{
	TInt unit = 0;
	while (aSize > 1024)
	{
		aSize /= 1024;
		unit++;
	}

	aBuf.AppendNum(aSize);

	switch (unit)
	{
		case 0:
		{
			aBuf.Append(_L(" B"));
			break;
		}
		case 1:
		{
			aBuf.Append(_L(" KB"));
			break;
		}
		case 2:
		{
			aBuf.Append(_L(" MB"));
			break;
		}
		case 3:
		{
			aBuf.Append(_L(" GB"));
			break;
		}
	}
}
// End of File
